package demoxsgl_300.com.gouwu1500.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import demoxsgl_300.com.gouwu1500.R;
import demoxsgl_300.com.gouwu1500.base.MyApplication;
import demoxsgl_300.com.gouwu1500.view.MyGridView;
import demoxsgl_300.com.gouwu1500.zhi.DialogActivity;
import demoxsgl_300.com.gouwu1500.zhi.FaZuoTeDian_1_Activity;

public class ListViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Activity context;
    private List<List<String>> list;
    private GridViewAdapter nearByInfoImgsAdapter;
    List<String> ccList = new ArrayList<String>();
    int head[] = {R.drawable.c1, R.drawable.c2, R.drawable.c3, R.drawable.c4,
            R.drawable.c5, R.drawable.c6, R.drawable.c7, R.drawable.c8,
            R.drawable.c9};
    String head2[] = {"常见病证", "肺系病证", "心系病证", "脾胃病证",
            "肝胆病证", "肾系病证", "气血津液病证", "肢体经络疾病",
            "其他疾病"};

    public ListViewAdapter(Activity context, List<List<String>> list) {
        super();
        this.mInflater = LayoutInflater.from(context);
        this.context = context;

        this.list = list;


    }

    @Override
    public int getCount() {

        return 9;


    }

    @Override
    public Object getItem(int arg0) {
        return list == null ? null : list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return list == null ? null : arg0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        if (list.size() == 0) {
            return null;
        }
        final ViewHolder holder;
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.item_listview, null);
            holder = new ViewHolder();


            holder.gv_images = (MyGridView) convertView.findViewById(R.id.gv_images);//图片
            holder.tv = (TextView) convertView.findViewById(R.id.tv);//图片
            holder.im = (ImageView) convertView.findViewById(R.id.imageView);//图片
            holder.cha = (TextView) convertView.findViewById(R.id.cha);//图片
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if ( list.get(position).size()<=6) {
            holder.cha.setVisibility(View.GONE);
        }
        nearByInfoImgsAdapter = new GridViewAdapter(context, list.get(position));
        holder.gv_images.setAdapter(nearByInfoImgsAdapter);

        holder.gv_images.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int positions, long id) {

                String s = list.get(position).get(positions);
//                Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
                context.startActivity(new Intent(context, FaZuoTeDian_1_Activity.class).putExtra("bb", s));
            }
        });
        holder.tv.setText(head2[position]);
        holder.im.setImageDrawable(MyApplication.getInstance().getResources().getDrawable(head[position]));
        holder.cha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> strings = list.get(position);
                ccList.clear();
                for (int i = 6; i < strings.size(); i++) {
                    ccList.add(strings.get(i));
                }

                Intent intent = new Intent(context, DialogActivity.class);
                intent.putStringArrayListExtra("qq", (ArrayList<String>) ccList);
                context.startActivity(intent);

            }
        });

        return convertView;
    }

    static class ViewHolder {

        MyGridView gv_images;
        TextView tv, cha;
        ImageView im;
    }


}

